const express = require("express");
const db = require("../model/db");
var router = express.Router();//注册路由


//查看所有MovieAndCinema对象
router.get("/one/:id", async (req, res) => {
    const data = await db.MovieAndCinema.find();
    res.send(data);
})

//作者：敬淋文
//先查询影院中已上映的电影
//获取某个影院中正在上映的所有电影id表示影院id
router.get("/:id", async (req, res) => {
    const cinemaId = req.params.id;
    const movies = await db.MovieAndCinema.find({ cinema: cinemaId }).populate("movie");
    const count = await db.MovieAndCinema.count({ cinema: cinemaId });
    res.send({
        "code": 0, //解析接口状态
        "msg": "", //解析提示文本
        count, //解析数据长度
        "data": movies //解析数据列表
    });
});

//作者:敬淋文
//获取影院中未上映的所有电影
router.get("/unshow/:id", async (req, res) => {
    const cinemaId = req.params.id;
    const obj = req.body;
    let movies = await db.MovieAndCinema.find({ cinema: cinemaId }).populate("movie"); //所有上映的电影 
    let arr = [];
    movies.map(item => {
        const id = item.movie._id;
        arr.push(id);
    })
    // 分页
    if (!obj.page) {
        req.query.page = 1;//页索引
    }
    if (!obj.limit) {
        req.query.limit = 10;//页容量
    }
    let condition = {};
    if (req.query.key) {
        const exp = new RegExp(req.query.key, "i")//不区分大小写，模糊查询的正则表达式
        condition = {
            $or: [
                { name: { $regex: exp } }
            ],
            _id: { $nin: arr }
        };
    } else {
        condition = { _id: { $nin: arr } }
    }
    const page = obj.page - 0;
    const limit = obj.limit - 0;
    let data = await db.Movie.find(condition, "-director -actor -createTime -updateTime -images"); //所有上映的电影 
    let count = await db.Movie.count(condition); //所有上映的电影 
    res.send({
        code: 0,
        msg: "",
        count,
        data: data
    });
})

//作者：敬淋文
//下映电影  需要知道下映电影的编号id
router.delete("/:id", async (req, res) => {
    const movie = req.params.id;
    const MovieAndCinema = await db.MovieAndCinema.deleteOne({
        movie
    })
    res.send({
        code: 0,
        message: "",
        data: "",
        count: ""
    });
    // console.log("删除完成")
})

//作者:敬淋文
//上映一个电影  需要知道影院的编号和上映电影的编号
router.post("/", async (req, res) => {
    const obj = req.body;
    const addmovie = await db.MovieAndCinema.create({
        movie: obj.movieid,//电影编号
        cinema: obj.cinemaid,//影院编号
        screens: []  //放映场次默认没有
    });
    res.send({
        code: 0,
        message: "",
        data: addmovie,
        count: ""
    });
})
//作者：敬淋文
//为电影添加一个放映场次 需要知道上映电影的编号id
router.put("/:id", async (req, res) => {
    const movie = req.params.id;
    const obj = req.body;    //添加的上映内容
    const MovieAndCinema = await db.MovieAndCinema.findById(movie);// 得到该上映的所有数据
    const _id = MovieAndCinema._id;
    MovieAndCinema.screens.push(obj);
    const data = await db.MovieAndCinema.updateOne({
        _id
    }, MovieAndCinema);
    res.send({
        code: 0,
        message: "",
        data: "",
        count: ""
    });
})

//作者：敬淋文
//删除一个放映 需要知道下映电影的编号id
router.post("/del", async (req, res) => {
    const movie = req.body.movie; //以上映电影的编号
    const delid = req.body.del; // 放映厅的编号
    const MovieAndCinema = await db.MovieAndCinema.findById(movie);// 得到该上映的所有数据
    const _id = MovieAndCinema._id;// 得到该上映电影的编号 方便后面修改
    const screens = MovieAndCinema.screens;
    for (let i = 0; i < screens.length; i++) {
        if (screens[i]._id == delid) {
            screens.splice(i, 1);
            break;
        }
    }
    const data = await db.MovieAndCinema.updateOne({
        _id
    }, MovieAndCinema);
    res.send({
        code: 0,
        message: "",
        data: "",
        count: ""
    });
    res.send(MovieAndCinema);
})
module.exports = router;
